package com.xzt.meeting.controller;

import com.xzt.meeting.entity.Dept;
import com.xzt.meeting.service.DeptService;
import com.xzt.meeting.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/7 14:13
 * @Version 1.0
 * @Introduce
 */

@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet{

    private DeptService deptService = new DeptService();

    private UserService userService = new UserService();

    public void findAllMap(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        List<Map<String, Object>> deptMapList = deptService.findAllMap();
        for (Map<String, Object> dept : deptMapList) {


            Long deptId = Long.parseLong(dept.get("id").toString());

            List<Map<String, Object>> userMapList = userService.findUserByDeptId(deptId);

            dept.put("userMapList",userMapList);
        }

        req.setAttribute("deptMapList",deptMapList);
        req.getRequestDispatcher("/html/department.jsp").forward(req,resp);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp){
        List<Dept> deptList = deptService.findAll();
        writerObjToString(resp,deptList);
    }
}
